package com.solarroast.mirrorarray.components;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.UIManager;

import com.solarroast.mirrorarray.SolarRoastConstants;
import com.solarroast.mirrorarray.beans.MAIBean;
import com.solarroast.mirrorarray.gui.MAIPopup;
import com.solarroast.mirrorarray.gui.MirrorArrayItemButton;
import com.solarroast.mirrorarray.gui.MirrorArrayItemWindow;
import com.solarroast.mirrorarray.gui.listeners.MAIClickListener;

public class MirrorArrayItem implements SolarRoastConstants {
	private MirrorArrayItemButton mArrayBut;
	private String mArrayName;
	private boolean active = false;
	private ArrayList<Mirror> mAIMirrors;
	private MirrorArrayItem thisMAI = this;
	private MirrorArray mArray;
	private MAIBean MAIInfo;
	
	public MirrorArrayItem(MirrorArray mArray, int numMirrors, int row, int col) {
		this.mArray = mArray;
		MAIInfo = new MAIBean(row, col);
		
		mAIMirrors = new ArrayList<Mirror>();
		for(int i = 0; i < numMirrors; i++) {
			mAIMirrors.add(new Mirror("" + i, this));
		}
		
		this.mArrayName = row + "-" + col;
		
		mArrayBut = new MirrorArrayItemButton(mArrayName, thisMAI, mArray);
		recalculateColor();
	}
	
	public JButton getButton() {
		return mArrayBut;
	}
	
	public int getActive() {
		int numActive = 0;
		for(int i = 0; i < mAIMirrors.size(); i++) {
			if(mAIMirrors.get(i).getActive())
				numActive++;
		}
		
		return numActive;
	}
	
	public StringBuffer getMirrorArrayStatus() {
		StringBuffer retStr = new StringBuffer("Mirror Array " + mArrayName + " Status\n");
		
		for(int i = 0; i < mAIMirrors.size(); i++) {
			retStr.append(mArrayName + "[" + i + "]: " + (mAIMirrors.get(i).getActive() ? "ONLINE" : "OFFLINE") + "\n");
		}
		
		return retStr;
	}
	
	public void printMirrorArrayStatus() {
		System.out.println("Mirror Array " + mArrayName + " Status");
		
		for(int i = 0; i < mAIMirrors.size(); i++) {
			System.out.println(mArrayName + "[" + i + "]: " + (mAIMirrors.get(i).getActive() ? "ONLINE" : "OFFLINE"));
		}
	}
	
	public int getNumMirrors() {
		return mAIMirrors.size();
	}
	
	public void recalculateColor() {
		if(getActive() == 4)
			mArrayBut.setForeground(LIME_GREEN);
		else if(getActive() == 3)
			mArrayBut.setForeground(STEEL_BLUE);
		else if(getActive() == 2)
			mArrayBut.setForeground(GOLD);
		else if(getActive() == 1)
			mArrayBut.setForeground(ORANGE_RED);
		else if(getActive() == 0)
			mArrayBut.setForeground(Color.DARK_GRAY);
	}

	public String getName() {
		return mArrayName;
	}
	
	public void toggleMirrorsActive(boolean active) {
		for(int i = 0; i < mAIMirrors.size(); i++) {
			mAIMirrors.get(i).setActive(active);
		}
		
		recalculateColor();
	}

	public ArrayList<Mirror> getArray() {
		return mAIMirrors;
	}
	
	public void toggleActive() {
		if(active) {
			active = false;
			getButton().setBackground(null);
			mArray.removeFromMap(MAIInfo);
		}
		else {
			active = true;
			getButton().setBackground(STEEL_BLUE);
			mArray.addToMap(MAIInfo);
		}
	}
	
	public boolean isActive() {
		return active;
	}
	
	public MAIBean getBean() {
		return MAIInfo;
	}
}
